<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="registerClassId">
        <el-select v-model="queryParams.registerClassId" placeholder="请输入注册类型" clearable>
          <el-option
            v-for="item in registerClassOptions"
            :key="item.id"
            :label="item.customerClass"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="checkStatus" >
        <el-select v-model="queryParams.checkStatus" placeholder="状态" clearable>
          <el-option label="待审核" value="0" />
          <el-option label="驳回" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="registerList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label="联系电话" align="center" prop="phoneNumber" />
      <el-table-column label="工作邮箱" align="center" prop="email" width="200"/>
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <el-table-column label="职务" align="center" prop="enterpriseName">管理员</el-table-column>
      <el-table-column label="国家-地区" align="center" prop="countryName" />
      <el-table-column label="注册类型" align="center" prop="registerClassName" />
      <el-table-column label="注册时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="checkStatus">
        <template slot-scope="scope">
          <span>{{ formatCheckStatus(scope.row.checkStatus) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleInfo(scope.row)"
          >查看</el-button>
          <el-button v-if="scope.row.checkStatus==='0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >审核</el-button>
          <el-button v-if="scope.row.checkStatus==='0'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleEditor(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改注册信息对话框 -->
    <el-dialog :title="title" :visible.sync="infoOpen" width="40%" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="姓名：" prop="realName">
          {{form.realName}}
        </el-form-item>
        <el-form-item label="联系电话：" prop="phoneNumber">
          {{form.phoneNumber}}
        </el-form-item>
        <el-form-item label="工作邮箱：" prop="email">
          {{form.email}}
        </el-form-item>
        <el-form-item label="企业名称：" prop="enterpriseName">
          {{form.enterpriseName}}
        </el-form-item>
        <el-form-item label="国家：" prop="countryName">
          {{form.countryName}}
        </el-form-item>
        <el-form-item label="类型：" prop="registerClassName">
          {{form.registerClassName}}
        </el-form-item>
        <el-form-item label="注册时间：" prop="createTime">
          {{parseTime(form.createTime)}}
        </el-form-item>
        <el-form-item label="状态：" prop="checkStatus">
          {{formatCheckStatus(form.checkStatus)}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelInfo">关闭</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="editopen" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="姓名：" prop="realName">
          {{form.realName}}
        </el-form-item>
        <el-form-item label="联系电话：" prop="phoneNumber">
          {{form.phoneNumber}}
        </el-form-item>
        <el-form-item label="工作邮箱：" prop="email">
          {{form.email}}
        </el-form-item>
        <el-form-item label="企业名称：" prop="enterpriseName">
          {{form.enterpriseName}}
        </el-form-item>
        <el-form-item label="注册类型：" prop="registerClassId">
          <el-select v-model="form.registerClassId" placeholder="请输入注册类型">
            <el-option
              v-for="item in registerClassOptions"
              :key="item.id"
              :label="item.customerClass"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="国家：" prop="countryId">
          {{form.countryName}}
        </el-form-item>
        <el-form-item label="状态：" prop="checkStatus">
          {{formatCheckStatus(form.checkStatus)}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="editCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改注册信息对话框 -->
    <el-dialog id="registerdialog" :title="title" :visible.sync="open" width="500px" append-to-body>
      <div v-loading="isLoading">
      <div   style="text-align: center;margin-bottom:35px">
        <span>请线下确认用户信息真实性，在下方执行审核操作</span>
      </div>
      <div slot="footer"  class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="checkNoPass">审核驳回</el-button>
        <el-button @click="checkPass">审核通过</el-button>
      </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRegister, getRegister, delRegister, addRegister, updateRegister,checkRegister,getRegisterOptionForEdit } from "@/api/system/register";

export default {
  name: "Register",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 注册信息表格数据
      registerList: [],
      registerClassOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      infoOpen: false,
      editopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        realName: null,
        phoneNumber: null,
        email: null,
        enterpriseName: null,
        registerClassId: null,
        countryId: null,
        passWord: null,
        checkStatus: null,
        checkUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        registerClassId: [
          { required: true, message: "注册类型不能为空", trigger: "blur" }
        ],
      },
      isLoading: false
    };
  },
  created() {
    this.getList();
    this.getRegisterClassOptions();
  },
  methods: {
    getRegisterClassOptions(){
      getRegisterOptionForEdit().then(response => {
        this.registerClassOptions = response.data;
      });
    },
    /** 查询注册信息列表 */
    getList() {
      this.loading = true;
      listRegister(this.queryParams).then(response => {
        this.registerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    formatCheckStatus(val){
      let statusName = "";
      switch (val){
        case "0":statusName="待审核";break;
        case "2":statusName="驳回";break
        default:break;
      }
      return statusName;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    editCancel() {
      this.editopen = false;
      this.reset();
    },
    cancelInfo() {
      this.infoOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        realName: null,
        phoneNumber: null,
        email: null,
        enterpriseName: null,
        registerClassId: null,
        countryId: null,
        passWord: null,
        checkStatus: null,
        checkUserId: null,
        delFlag: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加注册信息";
    },
    /** 修改按钮操作 */
    checkPass() {
      this.isLoading = true
      checkRegister({id:this.form.id,checkStatus:"1"}).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.isLoading = false
        this.open = false;
        this.getList();
      });
    },
    checkNoPass() {
      this.isLoading = true
      checkRegister({id:this.form.id,checkStatus:"2"}).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.isLoading = false
        this.open = false;
        this.getList();
      });
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRegister(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审核注册信息";
      });
    },
    handleEditor(row) {
      this.reset();
      const id = row.id || this.ids
      getRegister(id).then(response => {
        this.form = response.data;
        this.editopen = true;
        this.title = "修改注册信息";
      });
    },
    handleInfo(row) {
      this.reset();
      const id = row.id || this.ids
      getRegister(id).then(response => {
        this.form = response.data;
        this.infoOpen = true;
        this.title = "审核注册信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRegister(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.editopen = false;
              this.getList();
            });
          } else {
            addRegister(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.editopen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delRegister(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/register/export', {
        ...this.queryParams
      }, `register_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
